package com.wowo.day1;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;

import java.io.IOException;
import java.util.Date;
import java.util.List;

public class UserDaoTest1 {
    @Test
    public  void insertUserTest() throws IOException {
        //1.获得SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory =MyBatisUtil.getSqlSessionFactory();
        //2.获得SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3.调用SqlSession上的方法
        sqlSession.insert("zzc.insertUser",new User("zzc","qwe",25,new Date()));
        //4.提交
        sqlSession.commit();
        //5.关闭资源
        sqlSession.close();
    }

    @Test
    public void deleteUserTest() throws IOException {
        SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        sqlSession.delete("zzc.deleteUser",49);
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public  void updateUserTest() throws IOException {
        SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        sqlSession.update("zzc.updateUser",new User(51,"zzcz","asd",29,new Date()));
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void getUserByIdTest() throws IOException {
        SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user = sqlSession.selectOne("zzc.getUserById",51);
        //sqlSession.commit();
        sqlSession.close();
        System.out.println(user);
    }
    @Test
    public void getUsersTest() throws IOException {
        SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> users = sqlSession.selectList("zzc.getUsers");
        System.out.println(users);
        sqlSession.close();
    }
}
